<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr"><head><title>Classless scripting</title>



  <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <xml></xml>
  <link rel="stylesheet" type="text/css" href="MSDN.css"></head><body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
  <tbody>
    <tr id="hdr">
      <td class="runninghead">
      <p align="left">CS-Script 3.27.0</p>
      </td>
      <td class="product"> <br>
</td>
    </tr>
  </tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">Classless C# scripts</h1>
</div>
</div>
<br>
<div id="nstext"><font face="Arial" size="2"><span><span style="font-style: italic;">This feature has&nbsp;a full weight equivalent <a href="AutoClass.html">Autoclass</a>
functionality, which is a built-in feature of the CS-Script engine.It
is implemented as an internal precompiler thus it can be </span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">easily</span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">
improved by developing your custom "autoclass precompiler" instead. See
<a href="precompilers.html">Precompilers</a> chapter for details.</span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;"></span><span style="font-weight: bold;"><br></span></span></font><br>
<font face="Arial" size="2"><span><span style="font-weight: bold;"><br></span></span></font><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span>Sometimes it is desirable to have 
very light scripts with working code only and no code infrastructure. Fortunately, this can 
be accomplished by extending CS-Script without changing it's core 
implementation. And this has already been partially&nbsp;done in the earlier versions with the&nbsp;<span style="font-style: italic;">code.cs</span> script&nbsp;(see <a href="Script_library.html">Script Library</a>). This script&nbsp;allows&nbsp;execution of the&nbsp;C# code statements&nbsp;from the command-line 
without having&nbsp;any script file at all. <br>
<br>
The current version of the CS-Script comes with <span style="font-style: italic;">CSSCodeProvider.dll</span> assembly. It is an <a href="Alternative_Compilers.html">alternative compiler</a>, which can handle C#, VB, JScript, C++ and C# (classless C#) syntax. </span></font><font face="Arial" size="2"><span>This&nbsp;compiler makes decision which particular parser to engage at
run time on the base of the script file extension. The extension <span style="font-style: italic;">.ccs</span> will always&nbsp;trigger the usage of the</span></font><font face="Arial" size="2"><span> classless C#</span></font><font face="Arial" size="2"><span> (CC#) parser.<br>
<br>
The CC# parser is a very&nbsp;</span></font><font face="Arial" size="2"><span>primitive parser, which analysis the code in order
to detect
if it is written in&nbsp;classless or standard C# syntax. If&nbsp;the
script code is a&nbsp;classless C#&nbsp;it is injected into an empty class declaration and
obtained code is executed, otherwise the script is executed as usual. </span></font><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span><br>
<br>
Every thing what can be achieved in standard C# can be done with classless C# too.&nbsp;</span></font><font face="Arial" size="2"><span>As with standard C# scripts you can <a href="Importing_scripts.html">import</a> other scripts from you code. The important
thing to remember, when doing this,&nbsp;is that compiler
automatically adds namespace and class to the imported script. </span></font><font face="Arial" size="2"><span>Inserted namespace is the imported script file name without extension and the class name is normally&nbsp;"Script". </span></font><font face="Arial" size="2"><span>Thus if
you import the following script (<span style="font-style: italic;">imp.ccs</span>):<br>
<br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Print(<font color="#0000ff" size="2">string</font>&nbsp;text)<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(text);<br>
}</font></td>
    </tr>
  </tbody>
</table>
<br>
t<font face="Arial" size="2"><span>he compiler will interpret it as following: <br>
<br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font style="color: rgb(153, 153, 153);" color="#0000ff" size="2">using</font><span style="color: rgb(153, 153, 153);">&nbsp;System;</span><br style="color: rgb(153, 153, 153);">
      <br style="color: rgb(153, 153, 153);">
      <font style="color: rgb(153, 153, 153);" color="#0000ff" size="2">namespace</font><span style="color: rgb(153, 153, 153);">&nbsp;imp</span><br style="color: rgb(153, 153, 153);"><span style="color: rgb(153, 153, 153);">
{</span><br style="color: rgb(153, 153, 153);"><span style="color: rgb(153, 153, 153);">
&nbsp;&nbsp;&nbsp;&nbsp;</span><font style="color: rgb(153, 153, 153);" color="#0000ff" size="2">public</font><span style="color: rgb(153, 153, 153);"> </span><font style="color: rgb(153, 153, 153);" color="#0000ff" size="2">partial class</font><span style="color: rgb(153, 153, 153);">&nbsp;Script</span><br style="color: rgb(153, 153, 153);"><span style="color: rgb(153, 153, 153);">
&nbsp;&nbsp;&nbsp;&nbsp;{</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Print(<font color="#0000ff" size="2">string</font>&nbsp;text)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(text);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(153, 153, 153);">}</span><br style="color: rgb(153, 153, 153);"><span style="color: rgb(153, 153, 153);">
}</span></font></td>
    </tr>
  </tbody>
</table>
<br>
<font face="Arial" size="2"><span>So if you need to import <span style="font-style: italic;">imp.ccs</span> file it would need to be done this way (<span style="font-style: italic;">impTest.ccs</span>): <br>
<br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_import&nbsp;imp.ccs;<br>
      </font><br>auto_imp.Script.Print(<font color="#800000" size="2">"test"</font>);</font></td>
    </tr>
  </tbody>
</table>
<br>
<span style="font-weight: bold; font-style: italic;">Note:<br>
</span>
<ul style="font-style: italic;">
  <li>Any .ccs file can import both&nbsp;.cs and .ccs scripts, however .cs can do only another .cs files.</li>
  <li>
Remember that&nbsp;usage of&nbsp;namespaces can help to avoid any naming collisions.</li><li>Note
that by default the auto-generated namespace for the imported classless
script is a name of the script file (without extension) &nbsp;with
prefix "auto_". This is required to avoid potential problems when the
script file name starcts with a number (in C#&nbsp;namespace must start
with an alphabetic&nbsp;character).</li>
  <li>You can use ccs2cs.cs (<a href="Script_library.html">Script Library</a>) script to do conversion of any CC# script to the standard C# script.<br></li></ul>You&nbsp;can&nbsp;control&nbsp;how class infrastructure is generated by using <a href="non_engine_directives.html"><span style="font-style: italic;">//css_classless</span></a>
directive, which allows specifying class name (including namespace) for
class&nbsp;to be generated. &nbsp;You may need to use this feature when
hosting calssless script.<br><font face="Arial" size="2"><span><br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#008000" size="2">//css_classless&nbsp;MyNamespace.MyClass;<br><br></font><font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;SayHello(<font color="#0000ff" size="2">string</font>&nbsp;greeting)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(greeting);<br>}</font></span></font></font></span></font></font></span></font></font></span></font></td>
    </tr>
  </tbody>
</table>
<br>The script above will be interpreted at runtime as following:<br><font face="Arial" size="2"><span><br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#008000" size="2"><span style="color: rgb(153, 153, 153);">namespace MyNamespace<br>{<br>&nbsp;&nbsp;&nbsp; public partial class MyClass</span><br style="color: rgb(153, 153, 153);"><span style="color: rgb(153, 153, 153);">&nbsp; &nbsp; {</span><br></font><font color="#0000ff" size="2">&nbsp; &nbsp; &nbsp; &nbsp; static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;SayHello(<font color="#0000ff" size="2">string</font>&nbsp;greeting)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Console.WriteLine(greeting);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br></font></span></font></font></span></font></font></span></font></font></span></font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#008000" size="2"><span style="color: silver;">&nbsp;&nbsp;&nbsp;<span style="color: rgb(153, 153, 153);">}</span><br style="color: rgb(153, 153, 153);"><span style="color: rgb(153, 153, 153);">}</span></span></font></font></span></font></font></span></font></font></span></font></font></span></font></td></tr></tbody></table><br><br>
<span style="font-weight: bold;">Limitations</span><br>
<font face="Arial" size="2"><span>Classless C# syntax implies some
restrictions and limitations. Thus any file which contains classless C# must
consist of the following parts:</span></font><font face="Arial" size="2"><span><br>
</span></font>
<ul>
  <li><span style="font-style: italic;">Optional&nbsp;header</span><br style="font-style: italic;">
contains C# and CS-Script &nbsp;directives (e.g. <span style="color: rgb(51, 0, 153);">using System.IO; //css_reference CSScriptLibrary.dll;</span>)</li>
</ul>
<font face="Arial" size="2"><span></span></font>
<ul>
  <li><span style="font-style: italic;">Body</span><br style="font-style: italic;">
contains a free standing C# code without any class declarations</li>
</ul>
<font face="Arial" size="2"><span></span></font>
<ul>
  <li><span style="font-style: italic;">Optional footer <br>
    </span>contains additional class declarations and method definitions<span style="color: rgb(51, 0, 153); font-style: italic;"></span></li>
</ul>
<font face="Arial" size="2"><span>Because it is not possible to predict
future changes in the standard C# syntax or even due to the
implementation limitations it is possible that CC# parser may produce
incorrect outcome. For such cases special CC#</span></font><font face="Arial" size="2"><span> code markers&nbsp;</span></font><font face="Arial" size="2"><span>can be&nbsp;used to insure correct parsing. Such markers are used to indicate the start and the end of the </span></font><font face="Arial" size="2"><span>CC# </span></font><font face="Arial" size="2"><span>code <span style="font-style: italic;">Body</span> part&nbsp;and they are:<br>
<span style="font-style: italic;">
&nbsp;&nbsp;&nbsp; </span></span></font><span style="color: rgb(51, 0, 153); font-style: italic;"><span style="font-style: italic;"> </span></span><span style="color: rgb(51, 0, 153);">//css_begin;</span><span style="color: rgb(51, 0, 153); font-style: italic;"><br>
</span><font style="font-style: italic;" face="Arial" size="2"><span>&nbsp;&nbsp;&nbsp; </span></font><span style="color: rgb(51, 0, 153); font-style: italic;"> </span><span style="color: rgb(51, 0, 153);">//css_end;</span><br>
<font face="Arial" size="2"><span>The usage of these markers can be illustrated with the following code:<br>
<br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
      <br>
      <font color="#008000" size="2">//css_begin;<br>
      </font><br>
Print(<font color="#800000" size="2">"test"</font>);<br>
      <br>
      <font color="#008000" size="2">//css_end;<br>
      </font><br>
      <font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Print(<font color="#0000ff" size="2">string</font>&nbsp;text)<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(text);<br>
}</font></td>
    </tr>
  </tbody>
</table>
<font face="Arial" size="2"><span></span></font>
<h4 class="dtH4">Examples</h4>
<div style="text-align: center;">
<div style="text-align: left;">
<div><font face="Arial" size="2"><span>This is an example of the script, which sets </span></font><font face="Arial" size="2"><span>a Read-Only attribute to </span></font><font face="Arial" size="2"><span>all files from the current directory&nbsp;.<br>
<br>
</span></font>
<table style="width: 673px; text-align: left; height: 156px;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System.IO;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Windows.Forms;<br>
&nbsp;<br>
      <font color="#0000ff" size="2">string</font>&nbsp;msg&nbsp;=&nbsp;<font color="#800000" size="2">"Do&nbsp;you&nbsp;want&nbsp;to&nbsp;make&nbsp;all&nbsp;files&nbsp;in&nbsp;"</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Environment.CurrentDirectory + <font color="#800000" size="2">"&nbsp;Read-Only?"</font>;<br>
&nbsp;<br>
      <font color="#0000ff" size="2">if</font>&nbsp;(DialogResult.Yes&nbsp;==&nbsp;MessageBox.Show(msg,&nbsp;<font color="#800000" size="2">"CS-Script"</font>,&nbsp;MessageBoxButtons.YesNo))<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">foreach</font>&nbsp;(<font color="#0000ff" size="2">string</font>&nbsp;file&nbsp;in&nbsp;Directory.GetFiles(Environment.CurrentDirectory))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.SetAttributes(file,&nbsp;FileAttributes.ReadOnly);</font></td>
    </tr>
  </tbody>
</table>
<font face="Arial" size="2"><span><br>
This is a different edition of the same script. Now&nbsp;the classless C# code contains method definition</span></font><font face="Arial" size="2"><span>.<br>
</span></font>
<table style="width: 675px; text-align: left; height: 236px;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System.IO;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Windows.Forms;<br>
      <br>
      <font color="#0000ff" size="2">string</font>&nbsp;dir&nbsp;=&nbsp;Environment.CurrentDirectory;<br>
      <font color="#0000ff" size="2">string</font>&nbsp;msg&nbsp;=&nbsp;<font color="#800000" size="2">"Do&nbsp;you&nbsp;want&nbsp;to&nbsp;make&nbsp;all&nbsp;files&nbsp;in&nbsp;"</font>+dir+<font color="#800000" size="2">"&nbsp;Read-Only?"</font>;<br>
&nbsp;<br>
      <font color="#0000ff" size="2">if</font>&nbsp;(DialogResult.Yes&nbsp;==&nbsp;MessageBox.Show(msg,&nbsp;<font color="#800000" size="2">"CS-Script"</font>,&nbsp;MessageBoxButtons.YesNo))<br>
&nbsp;&nbsp;&nbsp;&nbsp;SetReadOnly(dir);<br>
      <br>
      <font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;SetReadOnly(<font color="#0000ff" size="2">string</font>&nbsp;dir)<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">foreach</font>&nbsp;(<font color="#0000ff" size="2">string</font>&nbsp;file&nbsp;in&nbsp;Directory.GetFiles(dir))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.SetAttributes(file,&nbsp;FileAttributes.ReadOnly);<br>
}</font></td>
    </tr>
  </tbody>
</table>
<p></p>
<font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span>This
is an example of the script </span></font><font face="Arial" size="2"><span>(the&nbsp;simplest possible)</span></font><font face="Arial" size="2"><span>, which prints the names of all <span style="font-style: italic;">.exe </span>files
from the current directory. This script does not have any <span style="font-style: italic; color: rgb(51, 0, 153);">using</span> directives at all (<span style="color: rgb(51, 0, 153);">using System</span>; is always assumed for all classless scripts).<br>
<br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">foreach</font>&nbsp;(<font color="#0000ff" size="2">string</font>&nbsp;file&nbsp;in&nbsp;System.IO.Directory.GetFiles(Environment.CurrentDirectory,&nbsp;<font color="#800000" size="2">"*.exe"</font>))<br>
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(file);</font></td>
    </tr>
  </tbody>
</table>
<font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span><br>
This
is another version of the script above, which illustrates the usage of classes in the classless scripts.<br>
<br>
</span></font><font face="Arial" size="2"><span></span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">new</font>&nbsp;Executables().Print();<br>

      <br>

      <font color="#0000ff" size="2">class</font>&nbsp;Executables<br>

{<br>

&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Print()<br>

&nbsp;&nbsp;&nbsp;&nbsp;{<br>
      </font><font face="Courier New" size="2"><font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var </font></font><font face="Courier New" size="2">files = System.IO.Directory.GetFiles(Environment.CurrentDirectory,&nbsp;<font color="#800000" size="2">"*.exe"</font>);</font><br>
      <font face="Courier New" size="2">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">foreach</font>&nbsp;(<font color="#0000ff" size="2">string</font>&nbsp;file&nbsp;in </font><font face="Courier New" size="2">files</font><font face="Courier New" size="2">)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(file);<br>

&nbsp;&nbsp;&nbsp;&nbsp;}<br>

}</font></td>
    </tr>
  </tbody>
</table>
</div>
</div>
</div>
<h4 class="dtH4"><br>
</h4>
<h4 class="dtH4">See Also</h4>
<br>
<a href="Alternative_Compilers.html">Alternative
compilers</a> |&nbsp;<a href="Script_library.html">Script Library</a> | <a href="Importing_scripts.html">Importing scripts</a><a href="ws.html"><span style="text-decoration: underline;"><br>
</span></a></div>
</body></html>